import {
	baseUrl
} from './api.js'

function request (url, data = {}, method = 'POST', contentType = 'application/json') {
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			data: data,
			method: method,
			header: {
				'Content-Type': contentType,
			},
			success (res) {
				console.log(res)
				resolve(res)
			},
			fail (res) {
				reject(res)
				console.log(res)
			}
		})
	})
}

function $gotoUrl (url) {
	uni.navigateTo({
		url: url
	});
}
function toast (msg, duration = 2000) {
	uni.showToast({
		icon: 'none',
		title: msg,
		duration
	})
}

function timestampToDate (timestamp, type = 'date') {
	const date = new Date(timestamp);
	const year = date.getFullYear();
	const month = ('0' + (date.getMonth() + 1)).slice(-2);
	const day = ('0' + date.getDate()).slice(-2);
	const hours = ('0' + date.getHours()).slice(-2);
	const minutes = ('0' + date.getMinutes()).slice(-2);
	const seconds = ('0' + date.getSeconds()).slice(-2);
	if (type == 'time') {
		return year + '-' + month + '-' + day + ' ' + hours + ":" + minutes + ":" + seconds;
	}
	return year + '-' + month + '-' + day;
}
module.exports = {
	timestampToDate,
	request,
	$gotoUrl,
	toast,
}